package com.itfreer.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.itfreer.ui.standard.IChildData;
import com.itfreer.ui.standard.IEditTextValidator;
import com.itfreer.ui.standard.ISimpleView;
import com.itfreer.ui.validations.EditTextValidator;
import com.itfreer.ui.validations.ValidationExecutor;
import com.itfreer.ui.validations.ValidationModel;
import com.itfreer.ui.validations.ValidationRegister;
import com.itfreer.utils.collection.SerializableMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提供基础表单Activity，重点解决表的数据录入问题
 */
public abstract class FormActivity extends BaseActivity {

    public static int STATE_ADD = 10;
    public static int STATE_EDIT = 20;
    public static int STATE_SAVE_SUCCESS = 30;

    private Map data;
    private EditTextValidator editTextValidator;

    public EditTextValidator getEditTextValidator(){
        return editTextValidator;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        editTextValidator = new EditTextValidator(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        initBundleData();

        initValidator();
    }

    /**
     * 初始化验证器
     */
    protected void initValidator(){
        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(View item : viewList){
            if(item instanceof IEditTextValidator){
                IEditTextValidator editTextValidator = (IEditTextValidator)item;
                String validatorName = editTextValidator.getValidatorName();
                if(validatorName!=null && validatorName!=""){
                    ValidationExecutor alidationExecutor = ValidationRegister.getValidationExecutor(validatorName);
                    if(alidationExecutor!=null){
                        getEditTextValidator().add(new ValidationModel((EditText) item, alidationExecutor, editTextValidator.getValidateTitle()));
                    }
                }
            }
        }
        getEditTextValidator().execute();
    }

    /**
     * 获取数据
     */
    protected void initBundleData(){
        Intent intent = getIntent();
        if(intent==null){
            return;
        }

        Bundle bundle = intent.getExtras();
        if(bundle==null){
            return;
        }

        SerializableMap serializableMap = (SerializableMap) bundle.get("map");
        if(serializableMap==null){
            return;
        }

        Map data = serializableMap.getMap();
        if(data!=null){
            setDataBind(data);
        }
    }

    /**
     * 绑定数据
     * @param dataMap
     */
    public void setDataBind(Map dataMap){
        this.data = dataMap;
        if(data==null){
            return;
        }

        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(final View item : viewList){
            if(item instanceof ISimpleView){
                ISimpleView simpleView = (ISimpleView)item;
                String fieldName = simpleView.getFieldName();
                if(fieldName!=null && fieldName!="" && data.containsKey(fieldName)){
                    Object value = data.get(fieldName);
                    simpleView.setFieldValue(value);
                }
            } else if(item instanceof IChildData){
                // 子表参数说明
                // childTables : {				子表信息
                // 		childName : {			子表名
                //			keyField			子表主键
                // 			orderObject 		主表的排序对象
                //			otherTName 			中间子表的第三方表名
                // 			otherRelation		中间子表与第三方表的关系
                //		}
                // }
                IChildData child = (IChildData)item;
                final String tableName = child.getChildTableName();
                Map<String, Map> childTables = new HashMap<String, Map>();
                Map childTable = new HashMap();
                Map orderObj = new HashMap();
                orderObj.put(child.getOrderFiledName(), child.getOrderType());
                childTable.put("orderObject", orderObj);
                childTables.put(tableName, childTable);

                // 这里需要改成本地数据请求处理
//                DataServer.dataServer.setContext(this);
//                DataServer.dataServer.getChild(data.get("tableName").toString(), data, childTables,
//                    new Response.Listener<Map>() {
//                        @Override
//                        public void onResponse(Map response) {
//                            data.put("childValues", response);
//                            onSetChildValues(item, data);
//                        }
//                    }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError error) {
//                        }
//                    }
//                );

            }
        }
    }

    /**
     * 获取数据
     */
    public Map getDataBind(){
        if(data==null){
            data = new HashMap();
        }

        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(View item : viewList){
            if(item instanceof ISimpleView){
                ISimpleView simpleView = (ISimpleView)item;
                String fieldName = simpleView.getFieldName();
                if(fieldName!=null && fieldName!=""){
                    data.put(fieldName, simpleView.getFieldValue());
                }
            } else if(item instanceof IChildData){
                IChildData child = (IChildData)item;
                String tableName = child.getChildTableName();
                onGetChildValues(item, data);
            }
        }
        return data;
    }

    /**
     * 设置子表数据
     */
    protected void onSetChildValues(View item, Map dataMap){

    }

    /**
     * 获取子表数据
     */
    protected void onGetChildValues(View item, Map dataMap){

    }
}